<?php
include("include/config.inc.php");

if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
  $incArray = 0;
  $logArray = 0;
  $selFId = 0 ;
  if((isset($_POST['fId']) && $_POST['fId'] > 0) || (isset($_POST['partyId']) && $_POST['partyId'] > 0) || (isset($_POST['incomingIdAuto']) && $_POST['incomingIdAuto'] > 0))
  {
  	$furnaceQuery   = NULL;
  	$partyIdQuery    = NULL;
  	$incomingIdQuery = NULL;
  	$fromDate = "'".$_POST['fromDateYear']."-".$_POST['fromDateMonth']."-".$_POST['fromDateDay']."'";
  	$toDate   = "'".$_POST['toDateYear']."-".$_POST['toDateMonth']."-".$_POST['toDateDay']."'";
  	
  	if(isset($_POST['fId']) && $_POST['fId'] > 0)
  	{
  		$furnaceQuery = " AND ( planning.fId1 = ".$_POST['fId']." || planning.fId2 = ".$_POST['fId']." || planning.fId3 = ".$_POST['fId'].") ";
  		$selFId = $_POST['fId'];
  	}
  	/**** Comment done for future requirement if client needs  
  	if(isset($_POST['partyId']) && $_POST['partyId'] > 0)
  	{
  		$partyIdQuery = " AND incoming.partyId = ".$_POST['partyId']." ";
  	}
  	if(isset($_POST['incomingIdAuto']) && $_POST['incomingIdAuto'] > 0)
  	{
  		$incomingIdQuery = " AND incoming.incomingId = ".$_POST['incomingIdAuto']." ";
  	}
  	******//////
    $selInc = "SELECT incoming.incomingId, partyName, DATE_FORMAT(IncomingDate,'%d-%m-%y') AS IncomingDate,
                      challan, pr1.processName AS process1,pr2.processName AS process2, pr3.processName AS process3, materialId,hardne,
                      planning.*,furnace1.furnaceName AS fName1,furnace2.furnaceName AS fName2,furnace3.furnaceName AS fName3
                 FROM incoming
                 LEFT JOIN party ON party.partyId = incoming.partyId
                 JOIN planning ON incoming.incomingId = planning.incomingId
                 LEFT JOIN process AS pr1 ON planning.processId1 = pr1.processId
                 LEFT JOIN process AS pr2 ON planning.processId2 = pr2.processId
                 LEFT JOIN process AS pr3 ON planning.processId3 = pr3.processId
                 LEFT JOIN furnace AS furnace1 ON planning.fId1 = furnace1.fId
                 LEFT JOIN furnace AS furnace2 ON planning.fId2 = furnace2.fId
                 LEFT JOIN furnace AS furnace3 ON planning.fId3 = furnace3.fId
                WHERE 1 = 1
                      ".$furnaceQuery."
                      ".$partyIdQuery."
                      ".$incomingIdQuery."
                ORDER BY incoming.incomingId ";
    $selIncRes = mysql_query($selInc);
    $incArray = array();
    $logArray = array();
    $inc = 0;
    while($incRow = mysql_fetch_array($selIncRes))
    {   
    	if($_POST['fId'] == $incRow['fId1'])
    	{
    	  $processNo = 1;
    	}
    	if($_POST['fId'] == $incRow['fId2'])
    	{
    	  $processNo = 2;
    	}
    	if($_POST['fId'] == $incRow['fId3'])
    	{
        $processNo = 3;
      }
        
    	$selLogsheet = "SELECT logsheet.*,logsheetDate , DATE_FORMAT(logsheetDate,'%d/%m') AS logsDate,qMName,
    	                       DATE_FORMAT(startTime,'%H:%i') AS startTime,
    	                       DATE_FORMAT(soakingStartTime,'%H:%i') AS soakingStartTime,
    	                       DATE_FORMAT(endTime,'%H:%i') AS endTime,planning.quantity,materialName
    	                  FROM logsheet
    	                  LEFT JOIN quenchingmedia ON logsheet.qMId = quenchingmedia.qMId
    	                  JOIN planning ON logsheet.planningId = planning.planningId
    	                  JOIN incoming ON planning.incomingId = incoming.incomingId
    	                  JOIN material ON incoming.materialId = material.materialId
    	                 WHERE logsheet.planningId = ".$incRow['planningId']."
    	                   AND logsheetDate >= ".$fromDate."
    	                   AND logsheetDate <= ".$toDate."
    	                   AND processNo = ".$processNo."
    	                 ORDER BY logsheet.logSheetId ";
      $selLogsheetRes = mysql_query($selLogsheet);
      $log = 0;
      while($logRow = mysql_fetch_array($selLogsheetRes))
      {
      	$incArray[$inc]['incomingId'] = $incRow['incomingId'];
      	$logArray[$inc][$log]['logSheetId']   = $logRow['logSheetId'];
      	$logArray[$inc][$log]['incomingId']   = $incRow['incomingId'];
      	$logArray[$inc][$log]['partyName']    = $incRow['partyName'];
      	$logArray[$inc][$log]['IncomingDate'] = $incRow['IncomingDate'];
      	$logArray[$inc][$log]['challan']      = $incRow['challan'];
      	$logArray[$inc][$log]['materialName'] = $logRow['materialName'];
      	$logArray[$inc][$log]['quantity']     = $logRow['quantity'];
      	$logArray[$inc][$log]['hardne']       = $incRow['hardne'];
      	
      	if($logRow['processNo'] == 1)
      	{
      		$logArray[$inc][$log]['process']     = $incRow['process1'];
      		$logArray[$inc][$log]['temp']        = $incRow['temp1'];
      		$logArray[$inc][$log]['soaking']     = $incRow['soaking1'];
      		$logArray[$inc][$log]['furnace']     = $incRow['fName1'];
      	}
      	if($logRow['processNo'] == 2)
      	{
      		
      		$logArray[$inc][$log]['process']     = $incRow['process2'];
      		$logArray[$inc][$log]['temp']        = $incRow['temp2'];
      		$logArray[$inc][$log]['soaking']     = $incRow['soaking2'];
      		$logArray[$inc][$log]['furnace']     = $incRow['fName2'];
      		
      	}
      	if($logRow['processNo'] == 3)
      	{
      		$logArray[$inc][$log]['process']     = $incRow['process3'];
      	  $logArray[$inc][$log]['temp']        = $incRow['temp3'];
      	  $logArray[$inc][$log]['soaking']     = $incRow['soaking3'];
      	  $logArray[$inc][$log]['furnace']     = $incRow['fName3'];
      	}
    	 
      	$logArray[$inc][$log]['logsheetNo']       = $logRow['logsheetNo'];
      	$logArray[$inc][$log]['logsheetDate']     = $logRow['logsDate'];
      	$logArray[$inc][$log]['startTime']        = $logRow['startTime'];
      	$logArray[$inc][$log]['startTemperature'] = $logRow['startTemperature'];
      	$logArray[$inc][$log]['soakingStartTime'] = $logRow['soakingStartTime'];
      	$logArray[$inc][$log]['endTime']          = $logRow['endTime'];
      	$logArray[$inc][$log]['endTemperature']   = $logRow['endTemperature'];
      	$logArray[$inc][$log]['qMName']           = $logRow['qMName'];
      	$logArray[$inc][$log]['quenchingTime']    = $logRow['quenchingTime'];
      	$logArray[$inc][$log]['tempBforQue']      = $logRow['tempBforQue'];
      	$logArray[$inc][$log]['tempAfterQue']     = $logRow['tempAfterQue'];
      	$logArray[$inc][$log]['notOkReason']      = $logRow['notOkReason'];
      	$logArray[$inc][$log]['pmrDone']          = $logRow['pmrDone'];
      	$logArray[$inc][$log]['pmMasterId']       = $logRow['pmMasterId'];
      	
      	if($logRow['status'] == 'outgoing' || $logRow['status'] == 1)
      	{
      		$logArray[$inc][$log]['status']         =  'Ok'; 
      	}
      	else if($logRow['status'] == 0)
      	{
      		$logArray[$inc][$log]['status']         =  'Not Ok'; 
      	}
      	else
      	{
      		$logArray[$inc][$log]['status']         =  'N D'; 
      	}
      	$log++;
      }
    	$inc++;
    }
  }
		
	$a = 0;
	$furnaceArray = array();
	$selectFurnace = "SELECT fId,furnaceName
	                    FROM furnace
	                   ORDER BY furnaceName";
	$selectFurnaceResult = mysql_query($selectFurnace);
	while($row = mysql_fetch_array($selectFurnaceResult))
	{
	  $furnaceArray['fId'][$a]   = $row['fId'];
	  $furnaceArray['furnaceName'][$a] = $row['furnaceName'];
	  $a++;
	}

	include("./bottom.php");
	$smarty->assign("selFId",$selFId);
	$smarty->assign("furnaceArray",$furnaceArray);
	$smarty->assign("incArray",$incArray);
	$smarty->assign("logArray",$logArray);
	$smarty->display("furnaceReport.tpl");
}
?>